package com.ruoyi.fantasy.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.huanzhidadi.fantasy.bean.domain.FantasyShopItem;
import com.ruoyi.fantasy.domain.FantasyShopItemBO;
import org.springframework.stereotype.Service;
import com.ruoyi.fantasy.mapper.FantasyShopItemMapper;
import com.ruoyi.fantasy.service.IFantasyShopItemService;
import com.ruoyi.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 商店道具关系 服务层实现
 * 
 * @author wanghui
 * @date 2020-01-29
 */
@Service
public class FantasyShopItemServiceImpl implements IFantasyShopItemService 
{
	@Resource
	private FantasyShopItemMapper fantasyShopItemMapper;

	/**
     * 查询商店道具关系信息
     * 
     * @param id 商店道具关系ID
     * @return 商店道具关系信息
     */
    @Override
	public FantasyShopItem selectFantasyShopItemById(Long id)
	{
	    return fantasyShopItemMapper.selectFantasyShopItemById(id);
	}
	
	/**
     * 查询商店道具关系列表
     * 
     * @param fantasyShopItem 商店道具关系信息
     * @return 商店道具关系集合
     */
	@Override
	public List<FantasyShopItem> selectFantasyShopItemList(FantasyShopItem fantasyShopItem)
	{
	    return fantasyShopItemMapper.selectFantasyShopItemList(fantasyShopItem);
	}
	
    /**
     * 新增商店道具关系
     * 
     * @param fantasyShopItem 商店道具关系信息
     * @return 结果
     */
	@Override
	public int insertFantasyShopItem(FantasyShopItem fantasyShopItem)
	{
	    return fantasyShopItemMapper.insertFantasyShopItem(fantasyShopItem);
	}
	
	/**
     * 修改商店道具关系
     * 输入基础数据，基础出卖价，当前数量，动态计算其他价格数据
     * @param fantasyShopItem 商店道具关系信息
     * @return 结果
     */
	@Override
	public int updateFantasyShopItem(FantasyShopItem fantasyShopItem)
	{
		// 基础收购价
		fantasyShopItem.setBasePurchasePrice(fantasyShopItem.getBaseSellPrice().divide(new BigDecimal(2)));
		// 当前价格比率
		BigDecimal priceRate = new BigDecimal(1)
				.subtract(new BigDecimal(fantasyShopItem.getCurQuantity() - fantasyShopItem.getBaseQuantity())
						.divide(new BigDecimal(fantasyShopItem.getBaseQuantity() * 2), 6, BigDecimal.ROUND_HALF_UP));
		// 动态调整当前出卖价和当前收购价
		BigDecimal curSellPrice = fantasyShopItem.getBaseSellPrice().multiply(priceRate);
		fantasyShopItem.setCurSellPrice(curSellPrice);
		fantasyShopItem.setCurPurchasePrice(fantasyShopItem.getCurSellPrice().divide(new BigDecimal(2)));
	    return fantasyShopItemMapper.updateFantasyShopItem(fantasyShopItem);
	}

	/**
     * 删除商店道具关系对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteFantasyShopItemByIds(String ids)
	{
		return fantasyShopItemMapper.deleteFantasyShopItemByIds(Convert.toStrArray(ids));
	}

	@Override
	public List<FantasyShopItemBO> selectAllocatedList(FantasyShopItemBO fantasyShopItemBO) {
		return fantasyShopItemMapper.selectAllocatedList(fantasyShopItemBO);
	}

	@Override
	public int insertAllocItems(String shopId, String itemIds) {
		String[] itemIdArray = Convert.toStrArray(itemIds);
		// 新增地图商店对应关系
		List<FantasyShopItem> list = new ArrayList<>();
		for (String itemId : itemIdArray)
		{
			FantasyShopItem fantasyShopItem = new FantasyShopItem();
			fantasyShopItem.setShopId(shopId);
			fantasyShopItem.setItemId(itemId);
			list.add(fantasyShopItem);
		}
		return fantasyShopItemMapper.insertAllocItems(list);
	}

	@Override
	public int deleteFantasyShopItem(FantasyShopItemBO fantasyShopItemBO) {
		return fantasyShopItemMapper.deleteFantasyShopItem(fantasyShopItemBO);
	}

	@Override
	public int deleteAllocItems(String shopId, String itemIds) {
		Long[] itemIdArray = Convert.toLongArray(itemIds);
		return fantasyShopItemMapper.deleteAllocItems(shopId, itemIdArray);
	}

	@Override
	public FantasyShopItemBO selectFantasyShopItemBOById(Long id) {
		return fantasyShopItemMapper.selectFantasyShopItemBOById(id);
	}

}
